package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.RedisConstant;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.Map;

/**
 * @author hql
 * @date 2019-12-04 12:52
 */
@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private JedisPool jedisPool;
    /**
     * 添加检查套餐信息及其对应的检查项信息
     * @param ids
     * @param setmeal
     */
    @Override
    public void addSetmeal(Integer[] ids, Setmeal setmeal) {
        setmealDao.addSetmeal(setmeal);
        Integer setmealId = setmeal.getId();
        if (ids!=null) {
            setmealDao.addMid(ids, setmealId);
        }
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
    }

    /**
     * 分页查询体检套餐
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findAllSetmeal(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());

        String queryString = queryPageBean.getQueryString();
        StringBuilder sb = new StringBuilder();
        if (queryString!=null){
            sb.append("%").append(queryString).append("%");
            queryString=sb.toString();
            queryPageBean.setQueryString(queryString);
        }

        List<Setmeal> setmealList= setmealDao.findAllSetmeal(queryPageBean);
        PageInfo<Setmeal> pageInfo = new PageInfo<>(setmealList);
        if (pageInfo.getLastPage()<queryPageBean.getCurrentPage()){
            queryPageBean.setCurrentPage(pageInfo.getLastPage());
            List<Setmeal> setmealList2= setmealDao.findAllSetmeal(queryPageBean);
            PageInfo<Setmeal> pageInfo2 = new PageInfo<>(setmealList2);
            long total = pageInfo2.getTotal();
            return new PageResult(total,setmealList2);

        }else {
            long total = pageInfo.getTotal();
            return new PageResult(total,setmealList);
        }


    }

    /**
     * 查询所有的检查套餐
     * @return
     */
    @Override
    public List<Setmeal> getAllSetmeal() {
        List<Setmeal> setmealList= setmealDao.getAllSetmeal();
        return setmealList;
    }

    /**
     * 根据id查询检查套餐跟检查组以及检查项的信息
     * @param id
     * @return
     */
    @Override
    public Setmeal findById(Integer id) {
        Setmeal setmeal = setmealDao.findById(id);
        return setmeal;
    }

    /**
     * 根据对应的id查询检查套餐的信息
     * @param id
     * @return
     */
    @Override
    public Setmeal findById2(Integer id) {
        Setmeal setmeal= setmealDao.findById2(id);
        return setmeal;
    }

    /**
     * 根据检查套餐id查询检查套餐的信息
     * @param id
     * @return
     */
    @Override
    public Setmeal findSetmeal(Integer id) {
        Setmeal setmeal= setmealDao.findSetmeal(id);
        return setmeal;
    }

    /**
     * 编辑修改检查套餐的信息
     * @param setmeal
     */
    @Override
    public void updateSetmeal(Setmeal setmeal) {
        setmealDao.updateSetmeal(setmeal);

    }

    /**
     * 删除检查套餐的信息
     * @param id
     */
    @Override
    public void delSetmeal(Integer id) {
        //删除中间表的信息，由于套餐信息是关系的最上层所以可以直接删除中间表，然后删除体检套餐
        setmealDao.delMid(id);
        //删除套餐信息
        setmealDao.delSetmeal(id);

    }

    /**
     * 查询所有的套餐名字跟对应的套餐的人数
     * @return
     */
    @Override
    public List<Map<String, Object>> findSetmealCount() {
        //查询所有的套餐名字跟对应的套餐的人数
        List<Map<String, Object>> mapList=setmealDao.findSetmealCount();


        return mapList;
    }
}
